﻿using System;
using System.Collections.Generic;
namespace havefun
{
    class Program
    {
        static void Main(string[] args)
        {
            Vector3 t1 = new Vector3(-5, -5, 0);
            Vector3 t2 = new Vector3(-5, 5, 0);
            Vector3 t3 = new Vector3(5, 5, 0);
            Vector3 t11 =new Vector3(-5, -5, 0);
            Vector3 t12 =new Vector3(5, -5, 0);
            Vector3 t13 = new Vector3(5, 5, 0);
            List<Vector3> vec3 = new List<Vector3>();
            vec3.Add(t1);
            vec3.Add(t2);
            vec3.Add(t3);
            vec3.Add(t11);
            vec3.Add(t12);
            vec3.Add(t13);

            var mat = new Matrix();
            mat.m14 = 40;
            mat.m24 = 15;
            float rotate = 0;
            float jump = 0;
            float scale = 0f;
            while (true)//主循环
            {
                rotate += 0.1f;//旋转
                jump += 2;//跳跃
                if (jump > 10) jump = 0;
                scale += 0.3f;
                if (scale > 2.0f) scale = 0f;
                float rscale = Math.Abs(scale - 1.0f) + 1.0f;//我眯眯眼
                mat.m11 = (float)Math.Cos(rotate) * rscale;
                mat.m12 = (float)-Math.Sin(rotate);
                mat.m21 = (float)Math.Sin(rotate);
                mat.m22 = (float)Math.Cos(rotate) / rscale;
                mat.m24 = 15 - Math.Abs(jump - 5);

                Console.Clear();//清除画面
                for (int i = 0; i < vec3.Count / 3; i++)//绘制模型
                {
                    var p0 = mat.Transform(vec3[i * 3]);
                    var p1 = mat.Transform(vec3[i * 3 + 1]);
                    var p2 = mat.Transform(vec3[i * 3 + 2]);

                    DrawTri(p0, p1, p2, i % 2 == 0 ? '&' : '0');
                }
                System.Threading.Thread.Sleep(100);
            }
        }
        static void DrawTri(Vector3 p0, Vector3 p1, Vector3 p2, char tag)
        {
            float minx = Math.Min(p0.x, p1.x);//求出三角形的包围盒
            float maxx = Math.Max(p0.x, p1.x);
            float miny = Math.Min(p0.y, p1.y);
            float maxy = Math.Max(p0.y, p1.y);
            minx = Math.Min(minx, p2.x);
            maxx = Math.Max(maxx, p2.x);
            miny = Math.Min(miny, p2.y);
            maxy = Math.Max(maxy, p2.y);
            for (int i = (int)minx; i < (int)maxx + 1; i++)//扫描包围盒
            {
                for (int j = (int)miny; j < (int)maxy + 1; j++)
                {
                    if (i < 0 || i >= 80) continue;//超出屏幕的不画
                    if (j < 0 || j >= 25) continue;
                    Vector3 p = new Vector3(i, j, 0);
                    if (Vector3.PointinTriangle(p0, p1, p2, p))//在三角型内？
                    {
                        Console.SetCursorPosition(i, j);//画一个字符（替代像素）
                        Console.Write(tag);
                    }
                }
            }
        }



    }
}